import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;

import javax.swing.*;
import java.awt.*;

/**
 * Created by jiao on 16/4/19.
 */
public class SpeakAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
//        Application application = ApplicationManager.getApplication();
//        MGComponent component = application.getComponent(MGComponent.class);
//        component.speak();

//        Messages.showMessageDialog("不使用Component", "标题", Messages.getInformationIcon());

//        Object navigatable = e.getData(CommonDataKeys.NAVIGATABLE);
//        if (navigatable != null) {
//            Messages.showDialog(navigatable.toString(), "Selected Element:", new String[]{"OK"}, -1, null);
//        }

        final DialogBuilder dialog = new DialogBuilder().title("自定义对话框");
//        dialog.resizable(false);
        dialog.setCenterPanel(getJComponent());
        dialog.setOkOperation(new Runnable() {
            @Override
            public void run() {
                T("点击确定了..");
            }
        });
        dialog.show();
    }

    private JComponent getJComponent() {
        JLabel username = new JBLabel("用户名:");
        JTextField _username = new JBTextField("1", 10);
        JLabel password = new JBLabel("密   码:");
        JTextField _password = new JBTextField();
        _password.setName("2");


        JPanel panel = new JPanel();
        panel.setSize(800, 600);
        panel.setLayout(new GridLayout(2, 2));

        panel.add(username);
        panel.add(_username);
        panel.add(password);
        panel.add(_password);

        return panel;
    }

    private void T(Object s) {
        Messages.showMessageDialog(String.valueOf(s), "蘑菇信息", Messages.getInformationIcon());
    }

//    @Override
//    public void update(AnActionEvent e) {
//        Project project = e.getProject();
//        if (project == null) {
//            return;
//        }
//        e.getPresentation().setEnabledAndVisible(e.getData(CommonDataKeys.NAVIGATABLE) != null);
//    }
}
